module MC.Order {
	export class kongzhitai {
		public constructor() {
			this.init();
		}
		private init() {
			$('[show]').click(function () {
				let show = $(this).attr('show');
				$('.conDiv').hide();
				$('.' + show).show();
			});

			$('.orderqingdanDiv .addbtn').click(() => {
				this.showAdd();
			});
			$('.msgboxadd .closebtn').click(() => {
				$('.msgboxadd').hide();
				$('.msgboxadd .contentdiv .ul li .value').val('');
			});
			let mthis = this;
			$('.ulyuanliao').on('click', 'li', function () {
				let li = $(this);
				if (li.hasClass('first')) {
					return;
				}
				mthis.showAdd(li);
			});

			$('.msgboxadd .contentdiv .butarray .savebtn').click(() => {
				this.save();
			});
			$('.msgboxadd .contentdiv .butarray .deletebtn').click(() => {
				this.deleteli();
			});

			$('.orderyunshuDiv .fahuobtn').click(()=>{
				this.fahuobtn();
			});

			$('.orderInfoDiv .querenshouhuobtn').click(()=>{
				this.querenshouhuo();
			});


			$('.orderqingdanDiv .buttonlist .send_yuanliao').click(() => {
				MC.msg.confirm("请检查无误,发送给工厂无法再修改!", () => {
					this.send_yuanliao(true);
				});
			});

			$('.orderKongzhiPage .orderwancheng').click(()=>{
				this.orderwancheng();
			});
			this.type=$('#type').val();
			this.status=$('#status').val();

			this.loadData();
			if(this.type=='gs'&&this.status==MC.DEL.UserOrdersModel.status_weifayuanliao){
				this.tishiFayuanliao();
			}

			if(this.type=='gc'&&this.status==MC.DEL.UserOrdersModel.status_jieshouyuanliao){
				this.tishiKaishishengchan();
			}

		}
		private type='';
		private status:any='';
		private nowLi: JQuery = null;
		private showAdd(nowLi: any = null) {
			let data: any = {};
			if (nowLi === null) {
				data.kuanhao = '';
				data.jianshu = 0;
				data.yuanliao = '';
				data.danwei = '';

				data.bao = 0;
				data.danhao = 0;
				data.sunhao = 0;
			} else {
				data = nowLi[0].json;
			}
			$('.contentdiv [att="kuanhao"] .value').val(data.kuanhao);
			$('.contentdiv [att="jianshu"] .value').val(data.jianshu);
			$('.contentdiv [att="yuanliao"] .value').val(data.yuanliao);
			$('.contentdiv [att="danwei"] .value').val(data.danwei);

			$('.contentdiv [att="bao"] .value').val(data.bao);
			$('.contentdiv [att="danhao"] .value').val(data.danhao);
			$('.contentdiv [att="sunhao"] .value').val(data.sunhao);
			this.nowLi = nowLi;
			$('.msgboxadd').show();
		}

		private save() {
			var data: any = {};
			data.kuanhao = $('.contentdiv [att="kuanhao"] .value').val();
			data.jianshu = $('.contentdiv [att="jianshu"] .value').val();
			data.yuanliao = $('.contentdiv [att="yuanliao"] .value').val();
			data.danwei = $('.contentdiv [att="danwei"] .value').val();
			data.bao = $('.contentdiv [att="bao"] .value').val();
			data.danhao = $('.contentdiv [att="danhao"] .value').val();
			data.sunhao = $('.contentdiv [att="sunhao"] .value').val();
			data.heji = data.jianshu * data.danhao;
			if (this.nowLi === null) {
				this.nowLi = $(`<li>
                        <span class="title kuanhao">款号</span>
                        <span class="title jishuan jianshu">件数</span>
                        <span class="title yuanliao">原料名称</span>
                        <span class="title danwei">单位</span>
                        <span class="title bao">匹/包/卷</span>
                        <span class="title jishuan danhao">单耗</span>
                        <span class="title sunhao">损耗</span>
                        <span class="title jishuan heji">总合计</span>
				</li>`);
				this.nowLi[0].json = data;
				$('.ulyuanliao').append(this.nowLi);
			}
			this.nowLi.find('.kuanhao').text(data.kuanhao);
			this.nowLi.find('.jianshu').text(data.jianshu);
			this.nowLi.find('.yuanliao').text(data.yuanliao);
			this.nowLi.find('.danwei').text(data.danwei);

			this.nowLi.find('.bao').text(data.bao);
			this.nowLi.find('.danhao').text(data.danhao);
			this.nowLi.find('.sunhao').text(data.sunhao + '%');

			this.nowLi.find('.heji').text(data.heji);
			this.nowLi[0].json = data;
			$('.msgboxadd').hide();
			$('.msgboxadd .contentdiv .ul li .value').val('');
			this.send_yuanliao(false);

		}

		private deleteli(){
			this.nowLi.remove();
			this.send_yuanliao(false);
			$('.msgboxadd').hide();
		}

		/** 生成json */
		private getjosn() {
			let list: any = $('.ulyuanliao li');
			let array = [];
			for (let i = 0; i < list.length; i++) {
				let obj = $(list[i]);
				if (!obj.hasClass('first')) {
					array.push(list[i].json);
				}
			}
			return array;
		}
		/** 发送给工厂 */
		private send_yuanliao(ispublic = false) {
			let array = this.getjosn();
			let data = {
				public: ispublic,
				array: array
			};
			MC.api('send_yuanliao', { data: data, out_trade_no: $('#out_trade_no').val() }, (json: any) => {
				///	console.log(json);
				MC.msg.show("更新成功");
				if(ispublic){
					$('.orderqingdanDiv .buttonlist,.msgboxadd .contentdiv .butarray .savebtn,.msgboxadd .contentdiv .butarray .deletebtn').hide();
				}
			});
		}



		/** 加载原料清单数据 */
		private loadData() {
			let jsonstr = $('#yuanliao').val();
			let json;
			if (jsonstr !== '') {
				json = JSON.parse(jsonstr);
				if(!json){
					return;
				}
				//console.log(json);
				for (let i = 0; i < json.length; i++) {
					this.addLi(json[i]);
				}
			}
		}

		private addLi(data: any) {
			let nowLi: any = $(`<li>
                        <span class="title kuanhao">款号</span>
                        <span class="title jishuan jianshu">件数</span>
                        <span class="title yuanliao">原料名称</span>
                        <span class="title danwei">单位</span>
                        <span class="title bao">匹/包/卷</span>
                        <span class="title jishuan danhao">单耗</span>
                        <span class="title sunhao">损耗</span>
                        <span class="title jishuan heji">总合计</span>
				</li>`);
			nowLi[0].json = data;
			if(!data.kuanhao){
				data.kuanhao='';
			}
			if(!data.jianshu){
				data.jianshu='';
			}
			if(!data.yuanliao){
				data.yuanliao='';
			}
			if(!data.danwei){
				data.danwei='';
			}
			if(!data.bao){
				data.bao='';
			}
			if(!data.danhao){
				data.danhao='';
			}
			if(!data.heji){
				data.heji='';
			}
			nowLi.find('.kuanhao').text(data.kuanhao);
			nowLi.find('.jianshu').text(data.jianshu);
			nowLi.find('.yuanliao').text(data.yuanliao);
			nowLi.find('.danwei').text(data.danwei);

			nowLi.find('.bao').text(data.bao);
			nowLi.find('.danhao').text(data.danhao);
			nowLi.find('.sunhao').text(data.sunhao + '%');

			nowLi.find('.heji').text(data.heji);
			$('.ulyuanliao').append(nowLi);
		}


		private fahuobtn(){
			let kd_code=$('.orderyunshuDiv .kd_code').val();
			let kd_name=$('.orderyunshuDiv .kd_name').val();
			let send_address=$('.orderyunshuDiv .send_address').val();
			let receiving_address=$('.orderyunshuDiv .receiving_address').val();
			MC.api('order_fabuo',{
				out_trade_no: $('#out_trade_no').val(),
				kd_code:kd_code,
				kd_name:kd_name,
				send_address:send_address,
				receiving_address:receiving_address,
			},(json:any)=>{
				MC.msg.show("设置成功"); 
			});
		}


		/** 到货签收 */
		private querenshouhuo(){
			MC.api('querenshouhuo',{
				out_trade_no: $('#out_trade_no').val()
			},(json:any)=>{
				 MC.msg.confirm("是否前往评价?",()=>{
					 window.open("/h5/order/shippingNewEvaluation.html?out_trade_no="+$('#out_trade_no').val(),'_self');
				 });
			});
		}

		/** 设置订单为完成状态 */
		private orderwancheng(){
			MC.msg.confirm("设置为完成状态后,订单的押金将会返还给工厂,该操作无法撤销,确定设置为完成吗?",()=>{ 
				MC.api('orderwancheng',{
					out_trade_no: $('#out_trade_no').val()
				},(json:any)=>{
					MC.msg.alert("设置成功");
				});
			});
		}


		/*** 提示用户发送原料 */
		private tishiFayuanliao(){
			MC.msg.confirm("请尽快给工厂发送原料清单",()=>{
				$('[show="orderqingdanDiv"]').click();
			});
		}

		/*** 提示工厂开始生产 */
		private tishiKaishishengchan(){
			MC.msg.confirm("是否设置开始生产状态",()=>{
				MC.api('Kaishishengchan',{
					out_trade_no: $('#out_trade_no').val()
				},()=>{
					MC.msg.alert("设置成功");
				});
			});
		}
	}
}
